package team.cdlzz.service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import team.cdlzz.dao.*;
import team.cdlzz.model.pojo.*;
import team.cdlzz.model.servicePojo.UploadProperties;
import team.cdlzz.util.IDUtils;
import team.cdlzz.util.UploadUtils;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author zw
 * @date 2022-06-25.
 */
@Service
@Log4j
public class CourseService {
    @Autowired
    CourseMapper courseMapper;
    @Autowired
    CourseDetailMapper courseDetailMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    FileDataMapper fileDataMapper;
    @Autowired
    UploadProperties uploadProperties;
    @Autowired
    ExamMapper examMapper;
    @Autowired
    SCMapper scMapper;
    @Autowired
    ExamPutMapper examPutMapper;

    public List<Course> getCourseList() {
        List<Course> courses = courseMapper.selectByPrimaryAll();
        for (Course course: courses) {
            course.setImg(IDUtils.getPath(course.getImg(),2));
        }
        return courses;
    }

//    public Map<Course, User> GetCourseInfoList() {
//        List<Course> courseList = courseMapper.selectByPrimaryAll();
//        Map<Course,User> map  = new HashMap<>();
//        for (Course course : courseList) {
//            User user =  userMapper.getTeacherInfo(course.getUid());
//            map.put(course,user);
//        }
//        return map;
//    }
    public List<Map<String, Object>> GetCourseInfoList() {
        PageHelper.startPage(1,7);
        List<Course> courseList = courseMapper.selectByPrimaryCountSize();
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Course course : courseList) {
            Map<String,Object> map  = new HashMap<>();
            User user =  userMapper.getTeacherInfo(course.getUid());
            course.setImg(IDUtils.getPath(course.getImg(),2));
            map.put("course",course);
            map.put("teacher",user);
            mapList.add(map);
        }
        return mapList;
    }

    /**
     * 匹对字符串
     * @param like
     * @param pageNo
     * @param pageSize
     * @return
     */
    public PageInfo getCourseInfo(String like,Integer pageNo,Integer pageSize){
        PageHelper.startPage(pageNo,pageSize);
        List<CourseDetail> list = courseDetailMapper.selectLikeName(like);
        Iterator<CourseDetail> iterator = list.iterator();
        for (CourseDetail courses :
                list) {
            Integer count = courseMapper.selectCount(courses.getCid());
            courses.setCount(count);
            courses.setcImg(IDUtils.getPath(courses.getcImg(),2));
        }
        PageInfo<CourseDetail> courseDetailPageInfo  = new PageInfo<>(list);
        return courseDetailPageInfo;
    }

    public CourseDetail getCoursebyID(Integer cid) {
        CourseDetail courseDetail = courseDetailMapper.selectByPrimaryKey(cid);
        courseDetail.setcImg(IDUtils.getPath(courseDetail.getcImg(),2));
        return courseDetail;
    }

    public List getCourseListFile(Integer cid) {
//        return fileData.selectByCid(cid);
        List<FileData> fileData = fileDataMapper.selectByCid(cid);
        List<User> list = new ArrayList<>();
        JSONObject jsonObject = new JSONObject();
        for (FileData fileDatum : fileData) {
            Map<String, Object> map = new HashMap<>();
            fileDatum.setPath(IDUtils.getPath(fileDatum.getFileName(),3));
            User user = userMapper.selectByPrimaryKey(fileDatum.getUid());
            fileDatum.setUsername(user.getName());
        }
        return fileData;
    }





    /**
     * 填充一个未创建的课程
     * @param uid
     * @param c_name
     * @param cInformation
     * @param filename
     * @return
     */
    public Course getDataSetCourse(Integer uid,  String c_name,
                            String cInformation,String filename){
        Course course = new Course();
        course.setImg(filename);
        course.setUid(uid);
        course.setCname(c_name);
        course.setInfo(cInformation);
        return course;
    }

    public CourseDetail getDataSetCourseDetail( Course course ,String t_name,
                            String t_detail, String startTime, String endTime,
                            String hourArrangement,String cDetails){
        CourseDetail courseDetail = new CourseDetail();
        courseDetail.setCourse(course);
        courseDetail.settIdentity(t_name);
        User user = userMapper.selectByPrimaryKey(course.getUid());
        courseDetail.settName(user.getName());
        courseDetail.settImg(user.getPhoto());
        courseDetail.settIdentity(t_detail);
        courseDetail.settOrganization(user.getOrganization());
        courseDetail.setcHours(hourArrangement);
        courseDetail.setcStarttime(startTime);
        courseDetail.setcDetail(cDetails);
        courseDetail.setcEndtime(endTime);
        return courseDetail;
    }

    public String  upImg(MultipartFile file){
        String name = file.getOriginalFilename();
        String fileName = UploadUtils.generateFileName(name); //获取文件名字
        try {
            file.transferTo(new File(uploadProperties.getPath_excel()+fileName));//写入文件
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return fileName;
    }

    @Transactional //升级为事务
    public String CreateCourse(Integer uid, String t_name, String c_name,
                               String t_detail, String startTime, String cDetails,
                               String endTime, String hourArrangement,
                               String cInformation, String file) {
        if (endTime.compareTo(startTime) < 0){
            return "时间不合理";
        }
        String filename = file;
        Course course = getDataSetCourse(uid, c_name,
                cInformation, filename);
        int i = courseMapper.getMaxCid();
        course.setCid(++i);
        courseMapper.insert(course); //插入
        CourseDetail courseDetail = getDataSetCourseDetail(course, t_name,
                 t_detail,  startTime,  endTime,
                 hourArrangement, cDetails);
        int insert = courseDetailMapper.insert(courseDetail);
        if (insert!=0){
            return "添加成功";
        }else {
            return "插入错误";
        }

    }

    public String createExam(Exam exam) {
        int insert = examMapper.insert(exam);
        if (insert!=1){
            return "系统错误";
        }else {
            return "创建成功";
        }
    }

    public List<Exam> ExamListByCid(Integer cid) {
        List<Exam> exams = examMapper.selectByCid(cid);

        return exams;
    }

    public String userSetCours(Integer uid, Integer cid) {
        SCKey scKey = new SCKey(cid, uid);
//        SCKey sc  =  scMapper.selectKey(cid,uid);
        System.out.println("尝试添加课程");
        System.out.println(uid+"===" +cid);
//        if (sc==null)
        int insert = scMapper.insert(scKey);
        if (insert!=0){
            return "加入成功";
        }else{
//            return "加入失败";
            return "已参加";

        }
    }

    public List<Exam> ExamListByCid(Integer cid, Integer uid) {

        List<Exam> exams = examMapper.selectByCid(cid);
        for (Exam exam : exams) {
            String s = examPutMapper.selectUserSetExam(exam.getEid(),uid);
            exam.setFlag(s);
        }
        return exams;
    }
}